
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.*;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

import lejos.pc.comm.*;

import java.io.*;

/**
 * A GUI that makes it possible to establish a Bluetooth connection
 * to an NXT controlled car. The car is then controlled by a power
 * and kiValueation value sent to the car. The tacho counter value is
 * received and displayed after each command to the car has been performed.
 * 
 * @author Ole Caprani 24.2.12
 *
 * 
 */
public class PCcarController extends JFrame implements ActionListener
{
   private TextField nameField = new TextField(12);
   private TextField addressField = new TextField(20);

   private String name = "Desire"; 
   private String address = "00165308d166";
   
   private TextField kp = new TextField(10);
   private TextField ki = new TextField(10);
   private TextField kd = new TextField(10);
   
   private JLabel power = new JLabel();
     
   private NXTComm nxtComm;
   private NXTInfo nxtInfo;
   private InputStream is;
   private OutputStream os;
   private DataInputStream dis;
   private DataOutputStream dos;
   
   private JButton connectButton = new JButton("Connect");   
   private JButton goButton = new JButton("Go");


   /**
    * Constructor builds GUI
    */
   public PCcarController() 
   {		
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setTitle("Control NXT");
      setSize(500,300);
      
      // holds labels and text fields
      JPanel p1 = new JPanel();  
      p1.add(new JLabel("Name:"));
      p1.add(nameField);
      nameField.setText(name);
      p1.add(new JLabel("Address:"));
      p1.add(addressField);
      addressField.setText(address);
     
      try
      {
         nxtComm = NXTCommFactory.createNXTComm(NXTCommFactory.BLUETOOTH);
      }
      catch (NXTCommException nce) {
      }
      nxtInfo = new NXTInfo();
     
      // holds connect button
      JPanel p2 = new JPanel();
      p2.add(connectButton);
      connectButton.addActionListener(this);
      p2.add(goButton);
      goButton.addActionListener(this);
      
      // holds labels and text fields
      JPanel p3 = new JPanel();  
      p3.add(new JLabel("kp:"));
      p3.add(kp);
      kp.setText("28");
      p3.add(new JLabel("ki:"));
      p3.add(ki);
      ki.setText("4");
      p3.add(new JLabel("kd:"));
	  p3.add(kd);
	  kd.setText("33");

      
      JPanel p5 = new JPanel();
      p5.add(new JLabel("Power:"));
      power.setText("0");
      p5.add(power);
      
      // North area of the frame
      JPanel panel = new JPanel();  
      panel.setLayout(new GridLayout(5,1));
      panel.add(p1);
      panel.add(p2);
      panel.add(p3);
      panel.add(p5);
      add(panel,BorderLayout.NORTH);

   }
   /**
    * Required by action listener; 
    * only action is generated by the get Length button
    */	
   public void actionPerformed(ActionEvent e)
   {
      if(e.getSource()== connectButton)
      {
         String name = nameField.getText();
         String address = addressField.getText();
         nxtInfo.name = name;
         nxtInfo.deviceAddress = address;
         try
         {
            nxtComm.open(nxtInfo);
            is = nxtComm.getInputStream();
            os = nxtComm.getOutputStream();
      	    dis = new DataInputStream(is);
      	    dos = new DataOutputStream(os);
         }
         catch (Exception ex) {
         }
      }	  
	   
      if(e.getSource()== goButton)
      {
    	 try
         {
    	    String kpString = kp.getText();
    	    int kpValue = new Integer(kpString).intValue();        
            dos.writeInt(kpValue);
            dos.flush();
    	    
            String kiString = ki.getText();
    	    int kiValue = new Integer(kiString).intValue();        
            dos.writeInt(kiValue);
            dos.flush();
            
            String kdString = kd.getText();
    	    int kdValue = new Integer(kdString).intValue();        
            dos.writeInt(kdValue);
            dos.flush();
            
            power.setText(" "+dis.readInt());
         }
         catch (Exception ex) {
         }           
      }
   }
   
   /**
    * Initialize the display Frame
    */		
   public static void main(String[] args)
   {
      PCcarController frame = new PCcarController();
      frame.setVisible(true);
   }
}	